package com.tensin.mall.product.webController;

import com.tensin.mall.product.entity.CategoryEntity;
import com.tensin.mall.product.service.CategoryService;
import com.tensin.mall.product.vo.Catalog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author Tensin
 * @version 1.0
 * @date 前端页面用的controller，用于页面跳转
 */

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    /**
     *
     * @param model springMvc提供的结构
     * @return 带分类的首页
     */
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){

        //TODO 1.查出所有的一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Category();

        model.addAttribute("categories",categoryEntities);
        //视图解析器自动拼，所以不用谢前缀和后缀
        return "index";
    }


    //index/catalog.json
    @ResponseBody //目的是为了发送数据而非跳转页面。
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catalog2Vo>> getCatalogJson(){
        Map<String, List<Catalog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }

}
